// pages/user/user.js

import api from '../../utils/api.js';
import { checkAndFetchToken, refreshToken, uploadImage, redirectToLogin } from '../../utils/Logic.js';
import { showModal, showToast } from '../../utils/Common.js';
import net from '../../utils/Request.js';
import storage from '../../utils/Storge.js';
import { tokenKey, userData } from '../../utils/constant.js';

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,

        itemInfo: [{
            id: 1,
            text: '统计今天所有订单'
        }, {
            id: 2,
            text: '取消当前用户今天订单'
        }, {
            id: 3,
            text: '修改密码'
        }, {
            id: 4,
            text: '退出登录'
        }, {
            id: 5,
            text: '关于'
        }, {
            id: 6,
            text: '修改密码'
        }, {
            id: 7,
            text: '修改密码'
        }, {
            id: 8,
            text: '修改密码'
        }, {
            id: 9,
            text: '修改密码'
        }, {
            id: 10,
            text: '修改密码'
        }]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('onload');
        checkAndFetchToken(this.route, this.fetchData);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        storage.getCache(userData).then(res => {
            that.setData({
                userInfo: res.data
            });
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.fetchData();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    fetchData: function () {
        var that = this;
        wx.showNavigationBarLoading();
        net.post(api.baseUrl + '/api/getUserInfo', {
            ...app.globalData.access_token
        }).then(res => res.data).then(res => {
            console.log('user info ', res);
            if (res.status === 20000) {
                // 
                that.setData({
                    userInfo: res.data
                });
                storage.setCache(userData, res.data);
            }
        }).catch(e => {
            if (e.statusCode === 401) {
                refreshToken(that.route);
            }
            console.error('get user info error ', e);
        }).then(() => {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        });
    },
    updateAvatar: function (e) {
        uploadImage(this.data.access_token);
    },


    itemClick: function (e) {
        var target = e.currentTarget.dataset.item;
        console.log(target);

        switch (target) {
            case 1:
                wx.navigateTo({
                    url: '../statistics/statistics'
                });
                break;
            case 2:
                this.cancleTodayOrder();
                break;
            case 3:
                wx.navigateTo({
                    url: '../updatePw/updatePw'
                })
                break;
            case 4:
                storage.removeCache(tokenKey).then(() => {
                    redirectToLogin();
                });
                break;
        }
    },

    cancleTodayOrder: function () {
        var that = this;
        showModal('提示', '确定取消今天的订单?', () => {
            wx.showNavigationBarLoading();
            net.post(api.baseUrl + 'api/cancelUserOrder', {
                'access_token': that.data.access_token
            }).then(res => res.data).then(res => {
                console.log(res);
                showToast(res.message);
            }).catch(e => {
                console.error('cancle order error', e);
            }).then(() => {
                wx.hideNavigationBarLoading();
            });
        });
    },


    updateUserInfo: function (e) {
        wx.navigateTo({
            url: '../userInfo/userInfo'
        });
    }
})